package models.my;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import models.user.UserInfo;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;
import utils.StringUtils;

/**
 * 用户积分充值记录
 * @author zhangpeng
 *
 */
@Entity
@Table(name="user_score_charge")
public class MyScoreCharge extends Model{

	private static final long serialVersionUID = 1L;

	@Id
	public long id;
	public String username;
	/**
	 * 充值交易号
	 */
	public String trans_no = StringUtils.getTransNo();
	/**
	 * 充值金额
	 */
	public String charge_num;
	/**
	 * 收费银行
	 */
	public String bank;
	/**
	 * 收费银行交易号
	 */
	public String bank_trans_no;
	/**
	 * 充值时间
	 */
	public long charge_time = StringUtils.getTimeStamp();
	
	
	public static MyScoreCharge getInstances(){
		return new MyScoreCharge();
	}
	
	public static Model.Finder<Long, MyScoreCharge> find = new Model.Finder<Long, MyScoreCharge>(Long.class, MyScoreCharge.class);
	
	/**
	 * 创建积分充值记录
	 * @param msc
	 * @return
	 */
	public boolean createMyScoreCharge(MyScoreCharge msc) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(msc.username)) {
			throw new MengException("110001", "用户名不存在");
		}

		try {
			msc.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createMyScoreCharge Exception," + t.getMessage());
			throw new MengException("110002", "创建积分充值记录失败");
		}
	}
	
	/**
	 * 获取积分充值记录历史列表
	 * @param page
	 * @param size
	 * @return
	 */
	public List<MyScoreCharge> getMyScoreCharge(String username, int page, int size) throws MengException{
		try {
			return find.where().eq("username", username).orderBy().desc("change_time").findPagingList(size).setFetchAhead(false).getPage(size).getList();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getMyScoreCharge Exception," + t.getMessage());
			throw new MengException("110005", "获取积分充值记录历史列表失败");
		}
	}
	
}
